package com.theBlind.orm;

import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

/**
 * @program: orm
 * @description: 返回值处理
 * @author: TheBlind
 * @create: 2021-04-23 15:57
 **/
public class ResultHandle {

    public <E> List<E> handlerResultSet(ResultSet resultSet, String className) {
        List<E> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                Class<?> aClass = Class.forName(className);
                E o = (E) aClass.newInstance();
                ResultSetMetaData metaData = resultSet.getMetaData();
                for (int i = 0; i < metaData.getColumnCount(); i++) {
                    String columnName = metaData.getColumnClassName(i);
                    Method method = aClass.getMethod("set" + columnName);
                    method.invoke(o, resultSet.getObject(i));
                }
                list.add(o);
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}
